﻿using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DLOrganizer
{
    public abstract class EnterpriseUtilities
    {
        public string Database { get; set; }
        public string StoredProcedure { get; set; }
        public Dictionary<string, object> Parameters { get; set; }

        public EnterpriseUtilities()
        {
            Parameters = new Dictionary<string, object>();
        }
        public DataSet ExecuteDataSet()
        {
            Database db = DatabaseFactory.CreateDatabase(Database);
            DbCommand dbcmd = db.GetStoredProcCommand(StoredProcedure);
            if (Parameters.Count != 0)
            {
                foreach (KeyValuePair<string, object> item in Parameters)
                {
                    db.AddInParameter(dbcmd, item.Key, DbType.Object, item.Value);
                }
            }
            DataSet ds = db.ExecuteDataSet(dbcmd);
            return ds;
        }
        public IDataReader ExecuteReader()
        {
            Database db = DatabaseFactory.CreateDatabase(Database);
            DbCommand dbcmd = db.GetStoredProcCommand(StoredProcedure);
            if (Parameters.Count != 0)
            {
                foreach (KeyValuePair<string, object> item in Parameters)
                {
                    db.AddInParameter(dbcmd, item.Key, DbType.Object, item.Value);
                }
            }
            IDataReader dr = db.ExecuteReader(dbcmd);
            return dr;
        }
        public void ExecuteQuery()
        {
            Database db = DatabaseFactory.CreateDatabase(Database);
            DbCommand dbcmd = db.GetStoredProcCommand(StoredProcedure);
            if (Parameters.Count != 0)
            {
                foreach (KeyValuePair<string, object> item in Parameters)
                {
                    db.AddInParameter(dbcmd, item.Key, DbType.Object, item.Value);
                }
            }
            db.ExecuteNonQuery(dbcmd);
        }
    }
}
